package com.ksd.modules.members.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.enums.StatusEnums;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.members.entity.AdminUserBenefits;
import com.ksd.modules.members.entity.AdminUserVip;
import com.ksd.modules.members.mapper.AdminUserVipMapper;
import com.ksd.modules.members.po.AdminUserVipSavePO;
import com.ksd.modules.members.po.AdminUserVipSearchPO;
import com.ksd.modules.members.service.IAdminUserBenefitsService;
import com.ksd.modules.members.service.IAdminUserVipService;
import com.ksd.modules.members.vo.AdminUserVipVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.stream.Collectors;

@Service
public class AdminUserVipServiceImpl extends BaseServiceImpl<AdminUserVipMapper, AdminUserVip> implements IAdminUserVipService {



    @Autowired
    private IAdminUserBenefitsService adminUserBenefitsService;


    /**
     * 查询列表
     * @param adminUserVipSearchPO
     * @return
     */
    public Page<AdminUserVipVo> findAdminUserVips(AdminUserVipSearchPO adminUserVipSearchPO){
        Page<AdminUserVip> page = new Page<>(adminUserVipSearchPO.getPageNo(),adminUserVipSearchPO.getPageSize());
        LambdaQueryWrapper<AdminUserVip> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminUserVip::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminUserVip::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserVipSearchPO.getVipType()), AdminUserVip::getVipType,adminUserVipSearchPO.getVipType());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserVipSearchPO.getStatus()), AdminUserVip::getStatus,adminUserVipSearchPO.getStatus());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminUserVipSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                            AdminUserVip::getTitle,adminUserVipSearchPO.getKeyword())
                    .or().like(AdminUserVip::getTag, adminUserVipSearchPO.getKeyword())

            );
        }

        lambdaQueryWrapper.orderByAsc(AdminUserVip::getSorted);
        Page<AdminUserVip> adminUserVipPage = this.page(page, lambdaQueryWrapper);
        Page<AdminUserVipVo> adminUserVipVoPage = tranferPageBo(adminUserVipPage, AdminUserVipVo.class);

        for (AdminUserVipVo record : adminUserVipVoPage.getRecords()) {
            String benefitsIds = record.getBenefitsIds();
            if (StringUtils.isNotEmpty(benefitsIds)) {
                List<Long> benefitsIdList = Arrays.stream(benefitsIds.split(","))
                        .map(Long::valueOf).collect(Collectors.toList());
                List<AdminUserBenefits> benefitsList = adminUserBenefitsService.lambdaQuery()
                        .in(AdminUserBenefits::getId, benefitsIdList)
                        .list();
                StringJoiner sj = new StringJoiner(",");
                for (AdminUserBenefits benefits : benefitsList) {
                    sj.add(benefits.getTitle());
                }
                record.setBenefitsStr(sj.toString());
            }
        }
        return adminUserVipVoPage;
    }

    /**
     * 回收站
     * @param adminUserVipSearchPO
     * @return
     */
    public Page<AdminUserVipVo> findAdminUserVipsRecyle(AdminUserVipSearchPO adminUserVipSearchPO){
        Page<AdminUserVip> page = new Page<>(adminUserVipSearchPO.getPageNo(),adminUserVipSearchPO.getPageSize());
        LambdaQueryWrapper<AdminUserVip> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminUserVip::getIsDeleted, DeletedEnums.DELETED.getCode());
        lambdaQueryWrapper.eq(AdminUserVip::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserVipSearchPO.getVipType()), AdminUserVip::getVipType,adminUserVipSearchPO.getVipType());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserVipSearchPO.getStatus()), AdminUserVip::getStatus,adminUserVipSearchPO.getStatus());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminUserVipSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                            AdminUserVip::getTitle,adminUserVipSearchPO.getKeyword())
                    .or().like(AdminUserVip::getTag, adminUserVipSearchPO.getKeyword())

            );
        }

        lambdaQueryWrapper.orderByAsc(AdminUserVip::getSorted);
        Page<AdminUserVip> adminUserVipPage = this.page(page, lambdaQueryWrapper);
        Page<AdminUserVipVo> adminUserVipVoPage = tranferPageBo(adminUserVipPage, AdminUserVipVo.class);
        return adminUserVipVoPage;
    }

    /**
     * 保存和更新
     * @param adminUserVipSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public AdminUserVipVo saveUpdateAdminUserVip(AdminUserVipSavePO adminUserVipSavePO){
        AdminUserVip adminUserVip = tranferBo(adminUserVipSavePO, AdminUserVip.class);
        adminUserVip.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminUserVip.setSystemName(SystemIdThrealLocal.get().getSystemName());
        boolean b = this.saveOrUpdate(adminUserVip);
        AdminUserVipVo adminUserVipVo = tranferBo(adminUserVip, AdminUserVipVo.class);
        return adminUserVipVo;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminUserVipVo getAdminUserVipById(Long id){
        AdminUserVip adminUserVip = this.getById(id);
        AdminUserVipVo adminUserVipVo = tranferBo(adminUserVip, AdminUserVipVo.class);
        return adminUserVipVo;
    }

    /**
     * 单个逻辑删除
     * @param id
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean deleteAdminUserVipById(Long id){
        boolean b =  this.updateStatus(id, AdminUserVip.Columns.IS_DELETED,1);
        return b;
    }

    /**
     * 单个恢复
     * @param id
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean recoverByIdAdminUserVip(Long id){
        boolean b =  this.updateStatus(id, AdminUserVip.Columns.IS_DELETED,0);
        return b;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminUserVipById(Long id){
        boolean b = this.removeById(id);
        return b;
    }

    /**
     * 批量逻辑删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean bacthDeletesAdminUserVip(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminUserVip> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted", DeletedEnums.DELETED.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        return true;
    }

    /**
     * 批量恢复
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRecoversAdminUserVip(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminUserVip> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted", DeletedEnums.UN_DELETED.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        return b;
    }

    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminUserVip(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchDowns(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminUserVip> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.UN_STATUS.getCode());
        updateWrapper.in("id", ids);
        return this.update(updateWrapper);
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchUps(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminUserVip> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.STATUS.getCode());
        updateWrapper.in("id", ids);
        return this.update(updateWrapper);
    }

}
